﻿using AspNetCoreWebApiRoute.Filter;
using AspNetCoreWebApiRoute.ModelBinder;
using AspNetCoreWebApiRoute.Module;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace AspNetCoreWebApiRoute
{
    [Route("api/[controller]/Actions")]
    [ApiController]
    public class IndexHtmlxxxx : ControllerBase
    {
        [HttpGet]
        public async void GetIndexHtml()
        {
            //var data = new FileStream(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "A2CDA41D-8572-45c2-A2FF-2A245C07B15D.png"), FileMode.OpenOrCreate,FileAccess.ReadWrite);
            //Response.ContentType = "application/octet-stream";
            //await data.CopyToAsync(Response.Body);
            //await Response.CompleteAsync();
            //data.Close();
            // 1、html文件返回

            var data = new FileStream(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "A2CDA41D-8572-45c2-A2FF-2A245C07B15D.png"), FileMode.OpenOrCreate, FileAccess.ReadWrite);
            Response.ContentType = "image/png";
            await data.CopyToAsync(Response.Body);
            await Response.CompleteAsync();
            data.Close();

            //return File(new FileStream(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "A2CDA41D-8572-45c2-A2FF-2A245C07B15D.png"), FileMode.Open),
            //        "image/png");
        }

        [HttpGet]
        [Route("Result")]
        public  Result Result()
        {
            return new Result();
        }

        /// <summary>
        /// 请求参数绑定
        /// </summary>
        /// <param name="ProductId"></param>
        /// <param name="Product"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        /// 
        //[MyActionFilter]   //第一种过滤方法
        //[ServiceFilter(typeof(MyActionFilterAttribute))] //第二种过滤方法 使用依赖注入方法可以注入一些必要的组件
        //[ServiceFilter(typeof(MyActionFilterxx))]
        [HttpGet("GetRouter/{Id:int=1}/{Name:double=1}/Path")]
        public string RouterAction([FromQuery(Name = "TonyId")] int ProductId, string Product, double Name)
        {
            return "路由Action";
        }

        /// <summary>
        /// 表单绑定
        /// </summary>
        /// <param name="ProductId"></param>
        /// <param name="Product"></param>
        /// <param name="Name"></param>
        /// <returns></returns>

        [HttpPost("GetRouter/{Id:int=1}/{Name:double=1}/Form")]
        public string RouterActionForm([FromForm] int ProductId, [FromForm] int xxx, string Product, double Name)
        {
            return "路由Action";
        }

        /// <summary>
        /// json绑定
        /// </summary>
        /// <param name="ProductId"></param>
        /// <param name="Product"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        [HttpPost("GetRouter/{Id:int=1}/{Name:double=1}/Json")]
        public string RouterActionJson([FromBody] int ProductId, string Product, double Name)
        {
            return "路由Action";
        }

        /// <summary>
        /// 对象Json绑定
        /// </summary>
        /// <param name="ProductId"></param>
        /// <param name="Product"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        [HttpPost("GetRouter/{Id:int=1}/{Name:double=1}/Object")]
        public string RouterActionObject([FromBody] Order order, double Name)
        {
            return "路由Action";
        }

        /// <summary>
        /// 对象表单绑定
        /// </summary>
        /// <param name="ProductId"></param>
        /// <param name="Product"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        [HttpPost("GetRouter/{Id:int=1}/{Name:double=1}/ObjectForm")]
        public string RouterActionObjectForm([FromQuery] Order order, string Product, double Name)
        {
            return "路由Action";
        }

        /// <summary>
        /// 集合对象绑定
        /// </summary>
        /// <param name="ProductId"></param>
        /// <param name="Product"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        [HttpPost("GetRouter/{Id:int=1}/{Name:double=1}/ListObject")]
        public string RouterActionListObject([FromBody] List<Order> orders, string Product, double Name)
        {
            return "路由Action";
        }

        /// <summary>
        /// 文件绑定
        /// </summary>
        /// <param name="ProductId"></param>
        /// <param name="Product"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        [HttpPost("GetRouter/{Id:int=1}/{Name:double=1}/File")]
        public string RouterActionFile([FromForm] Order formFile, string Product, double Name)
        {
            return "路由Action";
        }

        /// <summary>
        /// 集合文件绑定
        /// </summary>
        /// <param name="ProductId"></param>
        /// <param name="Product"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        [HttpPost("GetRouter/{Id:int=1}/{Name:double=1}/ListFile")]
        public string RouterActionListFile([FromForm] listFile formFiles, string Product, double Name)
        {
            return "路由Action";
        }

        /// <summary>
        /// Cookie绑定 自定义模型绑定
        /// </summary>
        /// <param name="ProductId"></param>
        /// <param name="Product"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        [HttpPost("GetRouter/{Id:int=1}/{Name:double=1}/Cookie")]
        public string RouterActionListCookie([FromCookie] User user, string Product, double Name)
        {

            Console.WriteLine("Action开始执行");
            throw new Exception("12321");
            return "路由Action";
        }

        [HttpGet]
        [Route("String")]
        public IActionResult GetString()
        {
            // 1、html文件返回
            return Ok("Tony讲webapi");
        }

        [HttpGet]
        [Route("Object")]
        public IActionResult GetObject()
        {
            // 1、html文件返回
            Product product = new Product();
            product.Id = 1;
            product.Name = "电脑";
            return Ok(product);
        }

        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Cookie")]
        public IActionResult GetCookie()
        {
            // 1、获取Cookie值 : HttpContext  HttpRequest
            string value = Request.Cookies["Tony"];

            return Ok(value);
        }

        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("Cookie")]
        public IActionResult PostCookie()
        {
            // 1、获取Cookie值 : HttpContext  HttpRequest
            Response.Cookies.Append("zhang", "所有客户端共享");

            return Ok("Cookie设置成功");
        }

        /// <summary>
        /// 获取Header
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Header")]
        public IActionResult GetHeader()
        {
            // 1、获取Header值 : HttpContext  HttpRequest
            string value = Request.Headers["Z"];

            return Ok(value);
        }

        /// <summary>
        /// 设置响应头Header
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("Header")]
        public IActionResult PostHeader()
        {
            // 1、设置响应Header头 : HttpContext  HttpRequest
            Response.Headers.Append("zhang-x", "123444");

            return Ok("Header设置成功");
        }
    }
}
